package com.wizardClient;

import com.appclient.DaoInstanceEnum;
import com.carpark.ejb.Model.ParkingspacesModel;
import com.configurator.SpaceButton;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.StackPane;
import spaces.Spaces;

/**
 *
 * @author Sawior
 */
public class ChooseParkingSpaceController implements Initializable, ClientControlledScreen {

    private ClientScreenController myController;
    @FXML
    private StackPane spaces;
    @FXML
    private SpaceButton actual;
    private Date startDate;
    private Date endDate;
    private String carType;

    public void loadSpacesStatus(Date startDate, Date endDate, String carType) {
        if (startDate == null || endDate == null || carType == null) {
            return;
        }
        this.startDate = startDate;
        this.endDate = endDate;
        this.carType = carType;
        createFreePlacesForDateAndCarType(startDate, endDate, carType);
    }

    private void createFreePlacesForDateAndCarType(Date startDate, Date endDate, String carType) {
        List<ParkingspacesModel> data = DaoInstanceEnum.INSTANCE.getDao().getParkingSpacesForReservation(startDate, endDate, carType);
        HashMap<Integer, ParkingspacesModel> goodSpace = new HashMap<>();
        for (ParkingspacesModel spaces : data) {
            goodSpace.put(spaces.getNumber(), spaces);
        }

        Spaces spacesGetter = new Spaces(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                actual = (SpaceButton) event.getSource();
            }
        }, Spaces.ENTRY, goodSpace, startDate, endDate, carType);
        if (spaces.getChildren().size() > 0) {
            spaces.getChildren().remove(0);

        }
        spaces.getChildren().add(0, spacesGetter.getSpacesForEntries());

    }

    @FXML
    private void initialize() {
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
    }

    @Override
    public void setScreenParent(ClientScreenController screenParent) {
        myController = screenParent;
    }

    @Override
    public boolean validateForm() {
        if (actual != null) {
            try {
                if (Integer.parseInt(actual.getId()) > 0) {
                    List<ParkingspacesModel> data = DaoInstanceEnum.INSTANCE.getDao().getParkingSpacesForReservation(startDate, endDate, carType);
                    if (data.contains(DaoInstanceEnum.INSTANCE.getDao().getParkingSpacesByNumber(Integer.parseInt(actual.getId())))) {
                        return true;
                    }
                }
            } catch (Exception ex) {
                return false;
            }
        }
        return false;
    }

    public ParkingspacesModel getChoosedSpace() {
        return DaoInstanceEnum.INSTANCE.getDao().getParkingSpacesByNumber(Integer.parseInt(actual.getId()));
    }
}
